//
//  ReadDetailViewController.m
//  leisure
//
//  Created by xalo on 16/4/20.
//  Copyright © 2016年 Mr.wangYongxu. All rights reserved.
//

#import "ReadDetailViewController.h"
#import "ReadListModel.h"

#define KReadDetailURL @"http://api2.pianke.me/article/info"

@interface ReadDetailViewController ()<UIWebViewDelegate>

@property(nonatomic, retain)UIWebView* webView;

@end

@implementation ReadDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDictionary* dic = @{@"contentid":self.model.contentid};
    [self fetchDatawithURl:KReadDetailURL parameter:dic];
    self.webView.paginationMode = UIWebPaginationModeTopToBottom;
    self.webView.gapBetweenPages = kViewHeight;
    self.webView.scalesPageToFit = NO;
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    [self createRightBarBtnItems];
}

//创建收藏和分享按钮
-(void)createRightBarBtnItems{

    UIBarButtonItem* shareBtn = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareAction:)];
    UIBarButtonItem* collectionBtn = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectionAction:)];
    self.navigationItem.rightBarButtonItems = @[shareBtn,collectionBtn];
}

-(void)shareAction:(UIBarButtonItem*)sender{

}

-(void)collectionAction:(UIBarButtonItem*)sender{

    [[DataManager defauleDataManager] InsertData:self.model];
}


-(UIWebView *)webView{
    
    if (!_webView) {
        _webView= [[UIWebView alloc] initWithFrame:self.view.bounds];
    }
    return _webView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth = 300.0;" // UIWebView中显示的图片宽度
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}

-(void)fetchDatawithURl:(NSString*)url parameter:(NSDictionary*)dic{
    
    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSString* htmlString = dic[@"data"][@"html"];
        NSLog(@"%@",[NSThread currentThread]);
       
        [self.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:kReadURL]];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
